package com.gc.ykqx.ui.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gc.ykqx.R;
import com.gc.ykqx.service.entity.Fore;
import com.gc.ykqx.service.presenter.ForePresenter;
import com.gc.ykqx.service.view.ForeView;
import com.gc.ykqx.service.util.Constant;
import com.gc.ykqx.service.util.ParseUtil;
import com.gc.ykqx.ui.custom.LazyFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 * 预报
 */
public class ForeFragment extends LazyFragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.stf_content)
    TextView tvSt;
    @BindView(R.id.sdf_content)
    TextView tvSd;
    @BindView(R.id.awf_content)
    TextView tvAw;
    private Unbinder unbinder;

    private static final String TAG = "ForeFragment";
    private Context mContext;
    private boolean isPrepared;
    private boolean isLoaded;
    private ForePresenter forePresenter;//预报


    public ForeFragment() {
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fore, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        isPrepared = true;
        lazyLoad();
        return view;
    }

    private void init() {
        mContext = getContext();
        forePresenter = new ForePresenter(mContext);
        forePresenter.onCreate();
        forePresenter.attachView(foreView);
        refreshLayout.setOnRefreshListener(ForeFragment.this);
    }

    private ForeView foreView = new ForeView() {
        @Override
        public void onSuccess(Fore fore, String foreType) {
            if (refreshLayout != null && refreshLayout.isRefreshing()) {
                refreshLayout.setRefreshing(false);
            }
            if (fore.getReturnCode() == Constant.Response.SUCCESS_CODE) {
                Log.e(TAG, "预报信息->: " + fore.toString());
                setFore(fore, foreType);
            }
        }

        @Override
        public void onError(String result) {

        }
    };

    private void setFore(Fore fore, String foreType) {
        try {
            String result = ParseUtil.parseFore(fore.getDS().get(0).getFILE_Text(), foreType);
            switch (foreType) {
                case Constant.Fore.SHORT_TIME:
                    tvSt.setText(result);
                    break;
                case Constant.Fore.SHORT_DATE:
                    tvSd.setText(result);
                    break;
                case Constant.Fore.A_WEEK:
                    tvAw.setText(result);
                    break;
            }
        } catch (Exception e) {
            Log.e(TAG, "setFore: " + e.getMessage());
        }
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || isLoaded) {
            return;
        }
        loadData();
        isLoaded = true;
    }

    @Override
    public void onRefresh() {
        loadData();
    }

    private void loadData() {
        forePresenter.getFore(Constant.Fore.SHORT_TIME);
        forePresenter.getFore(Constant.Fore.SHORT_DATE);
        forePresenter.getFore(Constant.Fore.A_WEEK);
    }
}
